package com.droidblackbox.receiver;

import org.json.JSONObject;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.droidblackbox.accident.AccidentLogo;
import com.droidblackbox.accident.AccidentReceiveInfoView;
import com.droidblackbox.info.SystemInfo;
import com.droidblackbox.manager.C2DMManager;
import com.droidblackbox.manager.FileManager;
import com.droidblackbox.manager.NotiManager;
import com.droidblackbox.manager.SystemManager;
import com.droidblackbox.manager.WASManager;

public class C2DMReceiver extends BroadcastReceiver {

	private final String REGISTRATION_ID = "registration_id";
	private final String UNREGISTERED = "unregistered";
	private final String ERROR = "error";
	private final String TAG = "C2DM";

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(C2DMManager.INTENT_REGISTRATION)) {
			handleRegistration(context, intent);
		} else if (intent.getAction().equals(C2DMManager.INTENT_RECEIVE)) {
			handleMessage(context, intent);
		}
	}

	private void handleRegistration(Context context, Intent intent) {
		String registration = intent.getStringExtra(REGISTRATION_ID);

		if (intent.getStringExtra(ERROR) != null) {
			Log.e(TAG, ERROR);
		}

		if (intent.getStringExtra(UNREGISTERED) != null) {
			Log.e(TAG, UNREGISTERED);
		}

		if (registration != null) {
			SystemInfo info = FileManager.getSystemInfo(context);
			info.setNumber(SystemManager.getInstance().getPhoneNumber(context));
			info.setRegistration(registration);
			FileManager.setSystemInfo(context, info);

			C2DMManager.getInstance().setRegistration(info.getRegistration());
			C2DMManager.getInstance().setNumber(info.getNumber());

			try {
				connPhoneInfo();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void handleMessage(Context context, Intent intent) {
		onReceiveActivity(context, intent);
	}

	private void onReceiveActivity(Context context, Intent receive) {

		String ticker = null;
		String string = receive.getStringExtra(WASManager.KEY_PUSH);
		final int flags = Notification.FLAG_ONGOING_EVENT
				| Notification.FLAG_SHOW_LIGHTS;

		try {
			JSONObject jsonObject = new JSONObject(string);
			ticker = jsonObject.getString(C2DMManager.KEY_PUSH_SUBMESSAGE).toString();
		} catch (Exception e) {
			// TODO: handle exception
		}

		NotiManager noti = new NotiManager(context);

		Intent intent = new Intent(context, AccidentLogo.class);
		intent.putExtra(C2DMManager.EXTRA_KEY, string);

		noti.setId(C2DMManager.ID_RECEIVE_NOTIFICATION);
		noti.setTicker(ticker);
		noti.setText(ticker);
		noti.setIntent(intent);
		noti.setFlags(flags);
		noti.onNotification();

		Intent intent2 = new Intent(context, AccidentReceiveInfoView.class);
		intent2.putExtra(C2DMManager.EXTRA_KEY, string);

		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intent2, PendingIntent.FLAG_ONE_SHOT);

		try {
			pendingIntent.send();
		} catch (CanceledException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void connPhoneInfo() throws Exception {

		final String number = C2DMManager.getInstance().getNumber();
		final String registration = C2DMManager.getInstance().getRegistration();
		WASManager.add(WASManager.KEY_HEADER, WASManager.HEADER_PHONE);
		WASManager.add(WASManager.KEY_BODY, WASManager.BODY_INSERT);
		WASManager.add(WASManager.KEY_NUMBER, number);
		WASManager.add(WASManager.KEY_REGISTRATION, registration);
		WASManager.add(WASManager.KEY_RECEIVE, WASManager.RECEIVE_ON);

		WASManager.conn();
	}
}
